package com.gandong8.trace.tool.config.dubbo;


import com.gandong8.trace.tool.config.GlobalProperties;
import com.gandong8.trace.tool.config.dubbo.properties.DubboProperties;

import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableConfigurationProperties({DubboProperties.class, GlobalProperties.class})
@Slf4j
//@EnableDubbo(scanBasePackages = "com.gandong8.trace.biz.service.impl")
public class DubboConf {
    @Autowired
    DubboProperties dubboProperties;
    @Autowired
    GlobalProperties globalProperties;

    /**
     * 当前应用配置
     * @return
     */
    @Bean
    public ApplicationConfig applicationConfig(){
        if(!globalProperties.getDubboOpen()) {
            System.err.println("dubbo not allowed");
            return null;
        }
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName(dubboProperties.getName());
        applicationConfig.setRegistry(registryConfig());
        log.info("----dubbo start!----");
        return applicationConfig;
    }

    /**
     * 连接注册中心配置
     * @return
     */
    private RegistryConfig registryConfig(){
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setProtocol(dubboProperties.getProtocol());
        registryConfig.setAddress(dubboProperties.getAddress());
        registryConfig.setCheck(dubboProperties.getCheck());
        registryConfig.setPort(dubboProperties.getPort());
        registryConfig.setTimeout(dubboProperties.getTimeout());
        return registryConfig;
    }

    /**
     * 服务提供者配置
     * @return
     */
    @Bean
    public ProviderConfig providerConfig() {
        if(!globalProperties.getDubboOpen()) {
            return null;
        }
        ProviderConfig providerConfig = new ProviderConfig();
        providerConfig.setFilter(dubboProperties.getFilter());
        providerConfig.setProtocol(protocolConfig());
        return providerConfig;
    }


    private ProtocolConfig protocolConfig() {
        ProtocolConfig protocolConfig = new ProtocolConfig();
        protocolConfig.setPort(dubboProperties.getPort());
        protocolConfig.setName("dubbo");
        return protocolConfig;
    }

    /**
     * 服务提消费者配置
     * @return
     */
    @Bean
    public ConsumerConfig consumerConfig() {
        if(!globalProperties.getDubboOpen()) {
            return null;
        }
        ConsumerConfig consumerConfig = new ConsumerConfig();
        consumerConfig.setFilter(dubboProperties.getFilter());
        return consumerConfig;
    }

    /**
     *
     * log4j:WARN No appenders could be found for logger (org.apache.dubbo.common.logger.LoggerFactory).
     * log4j:WARN Please initialize the log4j system properly.
     *  System.setProperty("dubbo.application.logger", "slf4j");
     */
    public void initLog() {
        //System.setProperty("dubbo.application.logger", "slf4j");
    }

}
